import { EggAppConfig, EggAppInfo, PowerPartial } from "egg";
import { join } from "path";

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;
  config.keys = appInfo.name + "_1631677352881_6029";
  // 全局中间件
  // config.middleware = ["myLogger"];
  // 应用本身的配置

  // console.log("process.ev", process.env);
  config.middleware = [];
  config.security = {
    csrf: {
      enable: false,
    },
  };

  config.view = {
    defaultViewEngine: "nunjucks",
  };

  config.logger = {
    consoleLevel: "DEBUG",
  };

  config.mongoose = {
    url: "mongodb://127.0.0.1:27017/lego",
  };

  config.bcrypt = {
    saltRounds: 10,
  };

  config.session = {
    encrypt: false,
  };

  config.oss = {
    client: {
      /* accessKeyId: process.env.ALC_ACCESS_KEY || "",
      accessKeySecret: process.env.ALC_SECRET_KEY || "",
      bucket: "lego-backend-lilong",
      endpoint: "oss-cn-shenzhen.aliyuncs.com", */
      accessKeyId: process.env.ALC_ACCESS_KEY || "LTAI5t8xwVEsUiuYKw5pchKd",
      accessKeySecret: process.env.ALC_SECRET_KEY || "WWfP9jKIMMic6mlWnbv2gtfho9GobS",
      bucket: "lego-backend-lilong",
      endpoint: "oss-cn-shenzhen.aliyuncs.com",
    },
  };

  config.multipart = {
    // mode: "file",
    // tmpdir: join(appInfo.baseDir, "uploads"),
    whitelist: [".png", ".jpg", ".gif", ".webp"],
    fileSize: "100kb",
  };

  config.jwt = {
    enable: true,
    secret: process.env.JWT_SECRET || "lilong",
    match: ["/api/users/getUserInfo", "/api/works", "/api/utils/upload-img", "/api/channel"],
  };

  config.static = {
    dir: [
      { prefix: "/public", dir: join(appInfo.baseDir, "app/public") },
      { prefix: "/uploads", dir: join(appInfo.baseDir, "uploads") },
    ],
  };

  // 业务逻辑配置
  const bizConfig = {
    sourceUrl: `http://github.com/eggjs/examples/tree/master${appInfo.name}`,
    myLogger: {
      allowMethod: ["POST"],
    },
    baseUrl: "default.url",
    /* mongoose: {
      url: "mongodb://127.0.0.1:27017/hello",
    }, */
  };

  // the return config will combines to EggAppConfig
  return {
    ...(config as {}),
    ...bizConfig,
  };
};
